package com.example.online_exam_system.service.impl;

import com.example.online_exam_system.dao.SubjectDao;
import com.example.online_exam_system.entity.Subject;
import com.example.online_exam_system.service.SubjectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * (Subject)表服务实现类
 *
 * @author lianglong@asiainfo.com
 * @since 2021-11-25 13:58:38
 */
@Service("subjectService")
public class SubjectServiceImpl implements SubjectService {
    @Resource
    private SubjectDao subjectDao;

    /**
     * 通过ID查询单条数据
     *
     * @param subjectid 主键
     * @return 实例对象
     */
    @Override
    public Subject queryById(Object subjectid) {
        return this.subjectDao.queryById(subjectid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Subject> queryAllByLimit(int offset, int limit) {
        return this.subjectDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param subject 实例对象
     * @return 实例对象
     */
    @Override
    public Subject insert(Subject subject) {
        this.subjectDao.insert(subject);
        return subject;
    }

    /**
     * 修改数据
     *
     * @param subject 实例对象
     * @return 实例对象
     */
    @Override
    public Subject update(Subject subject) {
        this.subjectDao.update(subject);
        return this.queryById(subject.getSubjectid());
    }

    /**
     * 通过主键删除数据
     *
     * @param subjectid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Object subjectid) {
        return this.subjectDao.deleteById(subjectid) > 0;
    }

    @Override
    public String querysubjectIdByName(String subjectName) {
        return this.subjectDao.querysubjectIdByName(subjectName);
    }

    @Override
    public String getSubjectIdByname(String subjectname) {
        return subjectDao.getSubjectIdByname(subjectname);
    }

    @Override
    public Map<String, Object> getSubjectNameById(String subjectid) {
        return subjectDao.getSubjectNameById(subjectid);
    }

    @Override
    public List<Map<String, Object>> getAllSubjects() {
        return subjectDao.getAllSubjects();
    }

    @Override
    public Map<String, Object> addSubject(Subject sub) {
        HashMap<String, Object> map = new HashMap<>();
        boolean flag1 = false;
        boolean flag2 = false;
        try {
            List<Subject> subjects = subjectDao.queryAll();
            for (Subject subject : subjects) {
                if (subject.getSubjectname().equals(sub.getSubjectname())&&subject.getStatus()==1){
                    flag1 = true;
                }else if (subject.getSubjectname().equals(sub.getSubjectname())&&subject.getStatus()==0){
                   flag2 = true;
                }
            }
            if (flag1){
                map.put("errMsg","该科目已存在");
                map.put("success",false);
            }else if (flag2){
                subjectDao.updateToActive(sub);
                map.put("data","添加成功");
                map.put("success",true);
            }else{
                subjectDao.insert(sub);
                map.put("data","添加成功");
                map.put("success",true);
            }
        }catch (Exception e){
            map.put("success",false);
            map.put("errMsg",e.getMessage());
            throw e;
        }
        return map;
    }

    @Override
    public Map<String, Object> updateSubStatus(Subject subject) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            int i = subjectDao.update(subject);
            //判断该科目是否已经失效，如果失效，修改条数为0
            if (i==0){
                map.put("data","该科目不存在");
            }
            map.put("data","删除成功");
            map.put("success",true);
        }catch (Exception e){
            map.put("success",false);
            map.put("errMsg",e.getMessage());
            throw e;
        }
        return map;
    }

    @Override
    public Map getSubjectNameByIdAready(String subjetId) {
        return subjectDao.getSubjectNameByIdAready(subjetId);
    }
}
